﻿using STHPMS1.BLL.B_Core;
using STHPMS1.ICore.I_BLL.Bus;
using STHPMS1.ICore.I_Entity.Info;
using STHPMS1.ICore.I_Entity.Stmpms1;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using STHPMS1.Common.C_Entity.Stmpms1;
using STHPMS1.ICore.I_Serv;
using STHPMS1.ICore;

namespace STHPMS1.BLL.B_Bus
{
    public class E_EvaluBus : BllBase, IE_EvaluBill
    {
        public void Save<T>(T entity)
            where T : class ,IE_Evalu
        {
            this.Scene.Dal.Sthpms1.Save(entity);
        }

        public void Remove(IE_Evalu entity)
        {
            this.Scene.Dal.Sthpms1.Delete(entity);
        }

        public IEnumerable<T> Get<T>(Expression<Func<T, bool>> predicate, IPagingInfo info)
            where T : class, IE_Evalu
        {
            return this.Scene.Dal.Sthpms1.Select<T>(predicate, info);
        }

        /// <summary>
        /// 保存路线评定结果
        /// </summary>
        /// <param name="inspecid">评定名称</param>
        /// <param name="rdwayid">路线编号</param>
        /// <param name="searchvalue">查询条件</param>
        public void Save(string inspecid,string rdwayid,string searchvalue)
        {
            ///TODO:路线中已经删除行车方向
            ////单独选取某个路线
            //if(rdwayid!=null)
            //{
            //    //根据路线表中获取行车方向 DIRE_ORNOT ：1全副，0上下行
            //    bool direction= this.Scene.Dal.Sthpms1.Single<B_Roadway>(q => q.Rdwayid == rdwayid).Dire_Ornot ?? false;
            //    if (direction)  //全副
            //    {
            //        Insert(inspecid, rdwayid, 13, searchvalue);
            //    }
            //    else //上下行
            //    {
            //        Insert(inspecid, rdwayid, 11, searchvalue);
            //        Insert(inspecid, rdwayid, 12, searchvalue);
            //    }
            //}
            //else //评定名称中所有路线
            //{
            //    List<string> list = this.Scene.Bll.E_Roadwaydetail.GetRdwayID(inspecid);
            //    foreach (string rdway in list)
            //    {
            //        //根据路线表中获取行车方向 DIRE_ORNOT ：1全副，0上下行
            //        bool direction = this.Scene.Dal.Sthpms1.Single<B_Roadway>(q => q.Rdwayid == rdway).Dire_Ornot ?? false;
            //        if (direction)  //全副
            //        {
            //            Insert(inspecid, rdway, 13, searchvalue);
            //        }
            //        else //上下行
            //        {
            //            Insert(inspecid, rdway, 11, searchvalue);
            //            Insert(inspecid, rdway, 12, searchvalue);
            //        }
            //    }
            //}

            this.Scene.Dal.Sthpms1.DeleteAll<E_Evalu>();
            if (rdwayid != null)
            {
                Insert(inspecid, rdwayid, searchvalue);
            }
            else
            {
                List<string> list = this.Scene.Bll.E_Roadwaydetail.GetRdwayID(inspecid);
                foreach (string rdway in list)
                {
                    Insert(inspecid, rdway, searchvalue);
                }
            }


        }



        public void Insert(string inspecid, string rdwayid,string searchvalue)
        {

            //this.Scene.Dal.Sthpms1.DeleteById<E_Evalu>(inspecid);

            E_Evalu info =new E_Evalu();
            IKeyGenrateService serv = MyApp.Locator.GetInstance<IKeyGenrateService>();
            info.Id = serv.NewGuid();
            info.Inspecid=inspecid;
            info.Rdwayid=rdwayid;

            //路网去掉
            //info.RoadnetName = this.Scene.Bll.B_Roadway.GetRoadnetName(info.Rdwayid);
            //行政区域去掉
            //info.Admdiv=this.Scene.Bll.B_Roadway.GetAdmdiv(rdwayid);
            //info.Admdiv = "";
            info.Rd_Class_Dictid=0; //技术等级
            info.Pvmttype_Dictid=0; //路面类型
            info.Evalu_Length = this.Scene.Bll.E_Evalu.GetLenght(inspecid, rdwayid, "") / 1000;  //评定长度

            if (info.Evalu_Length > 0)
            {
                info.Svydata = this.Scene.Dal.Sthpms1.SingleById<S_Inspeaccse>(inspecid).Year_M;  //评定时间
                //info.Direction_Dictid = d; //调查方向
                info.Manaut = "";  //管养单位
                info.Govern = ""; //主管单位  ??基础路段中没有



                //长度
                info.Gdlength_Mqi = this.Scene.Bll.E_Evalu.GetLenght(inspecid, rdwayid, "  and MQI>=80") / 1000;
                info.Mdlength_Mqi = this.Scene.Bll.E_Evalu.GetLenght(inspecid, rdwayid, "  and MQI>=70 and MQI<80") / 1000;
                info.Bdlength_Mqi = this.Scene.Bll.E_Evalu.GetLenght(inspecid, rdwayid, "  and MQI<70") / 1000;

                info.Gdlength_Pqi = this.Scene.Bll.E_Evalu.GetLenght(inspecid, rdwayid, "  and PQI>=80")/1000;
                info.Mdlength_Pqi = this.Scene.Bll.E_Evalu.GetLenght(inspecid, rdwayid, "  and PQI>=70 and PQI<80") / 1000;
                info.Bdlength_Pqi = this.Scene.Bll.E_Evalu.GetLenght(inspecid, rdwayid, "  and PQI<70") / 1000;

                info.Gdlength_Sci = this.Scene.Bll.E_Evalu.GetLenght(inspecid, rdwayid, "  and SCI>=80") / 1000;
                info.Mdlength_Sci = this.Scene.Bll.E_Evalu.GetLenght(inspecid, rdwayid, "  and SCI>=70 and SCI<80") / 1000;
                info.Bdlength_Sci = this.Scene.Bll.E_Evalu.GetLenght(inspecid, rdwayid, "  and SCI<70") / 1000;

                info.Gdlength_Tci = this.Scene.Bll.E_Evalu.GetLenght(inspecid, rdwayid, "  and TCI>=80") / 1000;
                info.Mdlength_Tci = this.Scene.Bll.E_Evalu.GetLenght(inspecid, rdwayid, "  and TCI>=70 and TCI<80") / 1000;
                info.Bdlength_Tci = this.Scene.Bll.E_Evalu.GetLenght(inspecid, rdwayid, "  and TCI<70") / 1000;


                info.Gdlength_Bci = this.Scene.Bll.E_Evalu.GetLenght(inspecid, rdwayid, "  and BCI>=80") / 1000;
                info.Mdlength_Bci = this.Scene.Bll.E_Evalu.GetLenght(inspecid, rdwayid, "  and BCI>=70 and BCI<80") / 1000;
                info.Bdlength_Bci = this.Scene.Bll.E_Evalu.GetLenght(inspecid, rdwayid, "  and BCI<70") / 1000;

                //路率

                info.Gdrdrate_Mqi = info.Gdlength_Mqi / (info.Evalu_Length )*100;
                info.Mdrdrate_Mqi = info.Mdlength_Mqi / (info.Evalu_Length) * 100;
                info.Bardrate_Mqi = info.Bdlength_Mqi / (info.Evalu_Length) * 100;

                info.Gdrdrate_Pqi = info.Gdlength_Pqi / (info.Evalu_Length) * 100;
                info.Mdrdrate_Pqi = info.Mdlength_Pqi / (info.Evalu_Length) * 100;
                info.Bardrate_Pqi = info.Bdlength_Pqi / (info.Evalu_Length) * 100;

                info.Gdrdrate_Sci = info.Gdlength_Sci / (info.Evalu_Length) * 100;
                info.Mdrdrate_Sci = info.Mdlength_Sci / (info.Evalu_Length) * 100;
                info.Bardrate_Sci = info.Bdlength_Sci / (info.Evalu_Length) * 100;

                info.Gdrdrate_Tci = info.Gdlength_Tci / (info.Evalu_Length) * 100;
                info.Mdrdrate_Tci = info.Mdlength_Tci / (info.Evalu_Length) * 100;
                info.Bardrate_Tci = info.Bdlength_Tci / (info.Evalu_Length) * 100;

                info.Gdrdrate_Bci = info.Gdlength_Bci / (info.Evalu_Length) * 100;
                info.Mdrdrate_Bci = info.Mdlength_Bci / (info.Evalu_Length) * 100;
                info.Bardrate_Bci = info.Bdlength_Bci / (info.Evalu_Length) * 100;


                //平均值MQI

                info.Avage_Mqi = this.Scene.Bll.E_Evalu.GetAverage(inspecid, rdwayid, "");

                if (info.Avage_Mqi >= 90)
                {
                    info.Evalucalss_Dictid = 255; //评定等级
                }
                if (info.Avage_Mqi >= 80 && info.Avage_Mqi < 90)
                {
                    info.Evalucalss_Dictid = 256; //评定等级
                }
                if (info.Avage_Mqi >= 70 && info.Avage_Mqi < 80)
                {
                    info.Evalucalss_Dictid = 257; //评定等级
                }
                if (info.Avage_Mqi >= 60 && info.Avage_Mqi < 70)
                {
                    info.Evalucalss_Dictid = 258; //评定等级
                }
                if (info.Avage_Mqi < 60)
                {
                    info.Evalucalss_Dictid = 259; //评定等级
                }
                this.Scene.Dal.Sthpms1.Insert<E_Evalu>(info);
            }

            
        }


        //public List<string> GetRoadnetName()
        //{
        //    return this.Scene.Dal.Sthpms1.E_Evalu.GetRoadnetName();
        //}
        //public List<string> GetYear()
        //{
        //    return this.Scene.Dal.Sthpms1.E_Evalu.GetYear();
        //}
        //public double? GetLenght(string roadnetname, string year)
        //{
        //    return this.Scene.Dal.Sthpms1.E_Evalu.GetLenght(roadnetname, year);
        //}
        //public double? GetAvageMQI(string roadnetname, string year)
        //{
        //    return this.Scene.Dal.Sthpms1.E_Evalu.GetAvageMQI(roadnetname, year);
        //}
        //public double? GetLenght(string roadnetname, string year, string searchvalue)
        //{
        //    return this.Scene.Dal.Sthpms1.E_Evalu.GetLenght(roadnetname, year, searchvalue);
        //}


        #region 获取指定条件所有单元的长度之和
        /// <summary>
        /// 获取指定条件所有单元的长度之和
        /// </summary>
        /// <param name="inspecid">评定单元</param>
        /// <param name="rdwayid">路线编号</param>
        /// <param name="d">调查方向</param>
        /// <param name="searchvalues">查询条件（ and MQI>=80）</param>
        /// <returns></returns>
        public double? GetLenght(string inspecid, string rdwayid, string searchvalues)
        {
            return this.Scene.Dal.Sthpms1.E_Evalu.GetLenght(inspecid, rdwayid, searchvalues);
        }
        #endregion

        #region 求检测数据的平均值（MQI求平均值：(长度*MQI+长度*MQI)/总长度  每个单元的长度乘以MQI / 各个单元长度之和）
        /// <summary>
        /// 求检测数据的平均值（MQI求平均值：(长度*MQI+长度*MQI)/总长度  每个单元的长度乘以MQI / 各个单元长度之和）
        /// </summary>
        /// <param name="inspecid">检测ID</param>
        /// <param name="rdwayid">路线编号</param>
        /// <param name="d">调查方向</param>
        /// <param name="searchvalues">(MQI,PQI,SCI,TCI,BCI)</param>
        /// <returns></returns>
        public double? GetAverage(string inspecid, string rdwayid, string searchvalues)
        {
            return this.Scene.Dal.Sthpms1.E_Evalu.GetAverage(inspecid, rdwayid, searchvalues);
        }
        #endregion
    }
}
